﻿[
    {
        $project: { "aof": "$$ROOT", "_id": 0 }
    },
    {
        $lookup: {
            localField: "aof.people",
            from: "peoples",
            foreignField: "_id",
            as: "p"
        }
    },
    {
        $unwind: {
            path: "$p",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $match: {
            $and: [
                { $and: [{ $and: [{ "aof.absence_type": { $eq: 'B' } }, { "aof.is_over": { $eq: true } }] }] },
                {
                    "aof.create_start_date": {
                        $lte: new ISODate('@CalcEndTime')
                    },
                    "aof.create_end_date": {
                        $gte: new ISODate('@CalcStartTime')
                    }
                }
            ]
        }
    },
    {
        $project: {
            "people_no": "$p.people_no", "people_name": "$p.people_name",
            "_id": "$aof._id", "create_start_date": "$aof.create_start_date",
            "create_end_date": "$aof.create_end_date", "hours": "$aof.hours",
            "reason": "$aof.reason", "work_on_time": "$aof.data.work_on_time",
            "work_off_time": "$aof.data.work_off_time", "time_zone_s": "$aof.data.time_zone_s",
            "time_zone_e": "$aof.data.time_zone_e", "total_time": "$aof.data.total_time",
            "conclusion": "$aof.data.conclusion", "effective_date": "$aof.data.effective_date",
        }
    }
]